package com.xuecheng.media.api;


import com.xuecheng.base.model.PageParam;
import com.xuecheng.content.model.po.PageResult;
import com.xuecheng.media.model.dto.UpLoadMediaFilesDto;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UpLoadMediaFilesParamDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
public class MediaFilesController {


  @Autowired
  MediaFileService mediaFileService;


 @ApiOperation("媒资列表查询接口")
 @PostMapping("/files")
 public PageResult<MediaFiles> list(PageParam pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
  Long companyId = 1232141425L;
  return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);

 }

 @ApiOperation("上传图片接口")
    @RequestMapping(value = "/upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UpLoadMediaFilesDto upload(@RequestPart("filedata") MultipartFile filedata,
@RequestParam(value= "objectName",required=false) String objectName) throws IOException {
     UpLoadMediaFilesParamDto paramDto = new UpLoadMediaFilesParamDto();
     paramDto.setFilename(filedata.getOriginalFilename());
     paramDto.setFileSize(filedata.getSize());
     paramDto.setFileType("001001");
         //创建临时文件
         File tempFile = File.createTempFile("minio", "temp");
         //上传的文件拷贝到临时文件
         filedata.transferTo(tempFile);
         //文件路径
         String filepath = tempFile.getAbsolutePath();

     Long companyId = 1232141425L;
     //上传文件
     UpLoadMediaFilesDto uploadfiles = mediaFileService.uploadfiles(companyId, paramDto, filepath,objectName);
     return uploadfiles;

 }
}
